package com.lavegue.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.tempuri.MMS;

import com.lavegue.common.Constants;
import com.lavegue.model.Msg;
import com.lavegue.model.MsgDetail;

/**
 * author: luoweibin
 * date:2012-1-10
 * description:
 *
 */
public class MsgSender {
	private static final String HEAD = "<smil xmlns=\"http://www.w3.org/2000/SMIL20/CR/Language\"><head><layout><root-layout width=\"208\" height=\"176\" /><region id=\"Image\" left=\"0\" top=\"0\" width=\"128\" height=\"128\" fit=\"hidden\" /><region id=\"Text\" left=\"0\" top=\"50\" width=\"128\" height=\"128\" fit=\"hidden\" /></layout></head><body>";
	private static final String FOOT = "</body></smil>";
	private String basepath;
	private String projectPath;
	private String zipSaveBasePath;
	public MsgSender(String projectPath) throws IOException {
		this.projectPath = projectPath;
		basepath = projectPath + "/msgpackage/";
		zipSaveBasePath = projectPath + "/zipdir/";
		new File(basepath).mkdirs();
		new File(zipSaveBasePath).mkdirs();
	}
	
	private String toZip(Msg msg) throws Exception {
		List<MsgDetail> details = msg.getMsgDetails();
		for (MsgDetail detail : details) {
			if (!StringUtil.isEmpty(detail.getPic())) {
				File f = new File(this.basepath + FilenameUtils.getName(detail.getPic()));
				FileUtils.copyFile(new File(projectPath + Constants.MSG_MOBAN_PIC_PATH + FilenameUtils.getName(detail.getPic())), f);
			}
			if (!StringUtil.isEmpty(detail.getMusic())) {
				File f = new File(this.basepath + FilenameUtils.getName(detail.getMusic()));
				FileUtils.copyFile(new File(projectPath + Constants.MSG_MOBAN_MUSIC_PATH + FilenameUtils.getName(detail.getMusic())), f);
			}
		}
		String zipFile = zipSaveBasePath + "bao" + new Random().nextInt(10000) + ".zip";
		new ZipTools().zip(zipFile,basepath , false);
		return zipFile;
	}
	
	private void toXml(Msg msg) throws IOException {
//		<par dur="25000ms">
//		<img src="1.jpg" region="Image" />
//		<text src="1.txt" region="Text" />
//		<audio src="1.mid" region="Audio" /> 
//		</par>
		StringBuffer sb = new StringBuffer();
		sb.append(HEAD);
		List<MsgDetail> details = msg.getMsgDetails();
		for (MsgDetail detail : details) {
			if (detail.getInter() == null) {
				sb.append("<par dur=\""+ 0 + "ms\">");
			} else {
				sb.append("<par dur=\""+ (detail.getInter()*1000) + "ms\">");
			}
			if (!StringUtil.isEmpty(detail.getPic())) {
				sb.append("<img src=\"" + FilenameUtils.getName(detail.getPic()) +"\" region=\"Image\"/>");
			}
			if (!StringUtil.isEmpty(detail.getTxt())) {
				sb.append("<text src=\"" + FilenameUtils.getName(createTxtFile(detail.getTxt())) +"\" region=\"Text\" />");
			}
			if (!StringUtil.isEmpty(detail.getMusic())) {
				sb.append("<audio src=\"" +  FilenameUtils.getName(detail.getMusic()) +"\" region=\"Audio\" /> ");
			}
			sb.append("</par>");
			
		}
		sb.append(FOOT);
		File smil = new File(basepath + "test.smil");
		smil.createNewFile();
		FileUtils.writeStringToFile(smil, sb.toString());
	}
	
	private String createTxtFile(String txt) throws IOException {
		String fielname = basepath + System.currentTimeMillis() + new Random().nextInt(1000) + ".txt";
		File f = new File(fielname);
		FileUtils.writeStringToFile(f, txt);
		return fielname;
	}
	
	
	public List<String> sendMsg(Msg msg, List<String> receivers) throws Exception {
		List<String> results = new ArrayList<String>();
		String bao = System.currentTimeMillis() + "/";
		this.basepath = this.basepath + bao;
		new File(this.basepath).mkdirs();
		toXml(msg);
		String zip = toZip(msg);
		MMS mms=new MMS();
		FileInputStream fs = null;
		try {
			fs = new FileInputStream(zip);
			byte[] Content=new byte[fs.available()];
			fs.read(Content,0,Content.length);
			for (String r : receivers) {
				try {
					String info = 	mms.getMMSSoap().sendMMS("SH-lwgkj", "26e87449f0", msg.getTitle(), r,Content , 2);
					System.out.println(info);
					System.out.println("------------------------------------------------------");
					//ok开头表示成功
					if (info.toLowerCase().startsWith("ok")) {
						results.add(r);
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fs != null) {
				fs.close();
			}
		}
		return results;
	}
	
	public static void test(String file) {
		List<String> receivers =  new ArrayList<String>();
		receivers.add("13724253120");
		MMS mms=new MMS();
		FileInputStream fs = null;
		try {
			fs = new FileInputStream(file);
			byte[] Content=new byte[fs.available()];
			fs.read(Content,0,Content.length);
			for (String r : receivers) {
				String info = 	mms.getMMSSoap().sendMMS("SH-lwgkj", "26e87449f0", "想死啊。。。。", r,Content , 2);
				System.out.println(info);
				System.out.println("------------------------------------------------------");
				//ok开头表示成功
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fs != null) {
				try {
					fs.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		
		MsgSender.test("E:/tomcat-6.0.29my/webapps/yicaitong_new/zipdir/test.zip");
//		System.out.println(System.getProperty("java.endorsed.dirs")); 
//		Msg msg = new Msg();
//		msg.setTitle("咋回事啊。。.");
//		List<MsgDetail> msgDetails = new ArrayList<MsgDetail>();
//		MsgDetail one = new MsgDetail();
//		one.setInter(1);
//		one.setTxt("第一帧内容。。。。fd附近的上课了发桑德菲杰看了");
//		MsgDetail two = new MsgDetail();
//		two.setPic("2.jpg");
//		msgDetails.add(one);
//		msgDetails.add(two);
//		msg.setMsgDetails(msgDetails );
//		
//		MsgSender s = new MsgSender("G:/temp/sendtest/");
//		
//		List<String> rs = new ArrayList<String>();
//		rs.add("13724253120");
//		s.sendMsg(msg, rs);
	}
}

